package com.mingqi.enetconnected.primary.mytask.adapter;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.mingqi.enetconnected.R;
import com.mingqi.enetconnected.data.bean.order.GoodsOrder;
import com.mingqi.enetconnected.primary.mytask.entity.MultipleEntity;
import com.mingqi.enetconnected.util.DeviceUtils;
import com.mingqi.enetconnected.view.decoration.DottedLineDecoration;

import java.util.List;

import static com.mingqi.enetconnected.primary.mytask.entity.MultipleEntity.ServiceContentEnum.GOODS_INFO;
import static com.mingqi.enetconnected.primary.mytask.entity.MultipleEntity.ServiceContentEnum.PRESSURE_TEST;
import static com.mingqi.enetconnected.primary.mytask.entity.MultipleEntity.ServiceContentEnum.SERVICE_ITEM;

/**
 * 任务详情-服务内容
 *
 * @author yaxi
 * @date 2018/9/13
 */
public class ServiceContentAdapter extends BaseMultiItemQuickAdapter<MultipleEntity,
        BaseViewHolder> implements TextWatcher, BaseQuickAdapter.OnItemChildClickListener, BaseQuickAdapter.OnItemClickListener {


    private ServiceItemAdapter mServiceItemAdapter;
    private GoodsInfoAdapter mGoodsInfoAdapter;

    private OnOperateListener mOperateListener;

    private RecyclerView mServiceRecycler;
    private RecyclerView mGoodsRecycler;
    private LinearLayout mServiceRecyclerContainer;
    private LinearLayout mGoodsRecyclerContainer;
    private LinearLayout mLlPressureTestContainer;

    //是否折叠的状态图标
    private ImageView mServiceIndicator;
    private ImageView mGoodsIndicator;
    private ImageView mPressureIndicator;

    public ServiceContentAdapter(List<MultipleEntity> data) {
        super(data);
        addItemType(SERVICE_ITEM.ordinal(), R.layout.item_service_item);
        addItemType(GOODS_INFO.ordinal(), R.layout.item_service_item);
        addItemType(PRESSURE_TEST.ordinal(), R.layout.item_pressure_test);

        setOnItemChildClickListener(this);
        setOnItemClickListener(this);
    }

    @Override
    protected void convert(BaseViewHolder helper, MultipleEntity item) {

        int itemViewType = helper.getItemViewType();
        helper.setText(R.id.item_service_item_title, item.getContent());

        if (itemViewType == SERVICE_ITEM.ordinal()) {

            helper.setVisible(R.id.item_service_item_add_to, true)
                    .setGone(R.id.item_service_item_inventory_selection, false)
                    .setGone(R.id.item_service_item_select_template, false)
                    .setGone(R.id.item_service_recycler_container, false)
                    .addOnClickListener(R.id.rl_service_item_title)
                    .addOnClickListener(R.id.item_service_item_add_to)
                    .addOnClickListener(R.id.item_service_item_indicator);

            mServiceRecycler = helper.getView(R.id.item_service_item_recycler);
            mServiceRecyclerContainer = helper.getView(R.id.item_service_recycler_container);
            mServiceIndicator = helper.getView(R.id.item_service_item_indicator);
            mServiceItemAdapter = new ServiceItemAdapter(item.getData());
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
            DottedLineDecoration dottedLineDecoration = new DottedLineDecoration(mContext);
            dottedLineDecoration.setDividerHeight(DeviceUtils.dp2px(mContext,1));
            dottedLineDecoration.setDottedLineCellWidth(DeviceUtils.dp2px(mContext,5));
            mServiceRecycler.addItemDecoration(dottedLineDecoration);
            mServiceRecycler.setLayoutManager(linearLayoutManager);
            mServiceRecycler.setAdapter(mServiceItemAdapter);

            mServiceItemAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
                @Override
                public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                    switch (view.getId()){
                        case R.id.iv_amount_edit:
                            if (mOperateListener != null){
                                mOperateListener.showServiceEditDialog(position, (GoodsOrder) adapter.getData().get(position));
                            }
                            break;
                            default:
                                break;
                    }
                }
            });


        } else if (itemViewType == GOODS_INFO.ordinal()) {

            helper.setGone(R.id.item_service_item_add_to, false)
                    .setVisible(R.id.item_service_item_inventory_selection, true)
                    .setVisible(R.id.item_service_item_select_template, true)
                    .setGone(R.id.item_service_recycler_container, false)
                    .addOnClickListener(R.id.rl_service_item_title)
                    .addOnClickListener(R.id.item_service_item_inventory_selection)
                    .addOnClickListener(R.id.item_service_item_select_template)
                    .addOnClickListener(R.id.item_service_item_indicator);

            mGoodsRecycler = helper.getView(R.id.item_service_item_recycler);
            mGoodsRecyclerContainer = helper.getView(R.id.item_service_recycler_container);
            mGoodsIndicator = helper.getView(R.id.item_service_item_indicator);
            mGoodsInfoAdapter = new GoodsInfoAdapter(item.getData(), new GoodsInfoAdapter.OnGoodsEditClickedListener() {
                @Override
                public void onGoodsEdit(int position,GoodsInfoAdapter adapter) {
                    if (mOperateListener != null){
                        mOperateListener.showGoodsEditDialog(position, adapter.getData().get(position));
                    }
                }
            });
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
            DottedLineDecoration dottedLineDecoration = new DottedLineDecoration(mContext, DeviceUtils.dp2px(mContext,15),DeviceUtils.dp2px(mContext,15));
            dottedLineDecoration.setDividerHeight(DeviceUtils.dp2px(mContext,1));
            dottedLineDecoration.setDottedLineCellWidth(DeviceUtils.dp2px(mContext,5));
            mGoodsRecycler.addItemDecoration(dottedLineDecoration);
            mGoodsRecycler.setLayoutManager(linearLayoutManager);
            mGoodsRecycler.setAdapter(mGoodsInfoAdapter);
            mGoodsInfoAdapter.bindToRecyclerView(mGoodsRecycler);



        } else if (itemViewType == PRESSURE_TEST.ordinal()) {
            helper.addOnClickListener(R.id.item_service_item_indicator)
                    .addOnClickListener(R.id.rl_pressure_title);
            helper.setGone(R.id.ll_pressure_test_container,false);
            mPressureIndicator = helper.getView(R.id.item_service_item_indicator);
            EditText etInitialPressure = helper.getView(R.id.et_initial_pressure);
            EditText etEndPressure = helper.getView(R.id.et_end_pressure);
            EditText etOperatingPressure = helper.getView(R.id.et_operating_pressure);
            EditText etHangingTime = helper.getView(R.id.et_hanging_time);
            EditText etStaticGasSupplyPressure = helper.getView(R.id.et_static_gas_supply_pressure);
            EditText etDynamicGasSupplyPressure = helper.getView(R.id.et_dynamic_gas_supply_pressure);


            etInitialPressure.addTextChangedListener(this);

            mLlPressureTestContainer = helper.getView(R.id.ll_pressure_test_container);

        }

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        Log.i(TAG, "beforeTextChanged: ");
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        Log.i(TAG, "onTextChanged: ");
    }

    @Override
    public void afterTextChanged(Editable s) {
        Log.i(TAG, "afterTextChanged: ");
    }


    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        Log.i(TAG, "onItemChildClick: ");

        if (mOperateListener != null) {
            mOperateListener.onRequestFocus();
        }
        switch (view.getId()) {
            case R.id.item_service_item_add_to:
                if (mOperateListener != null) {
                    GoodsOrder goodsOrder = mOperateListener.onServiceContentAddTo();
                    if (goodsOrder != null) {
                        mServiceItemAdapter.addData(0, goodsOrder);
                    }
                }
                break;
            case R.id.item_service_item_inventory_selection:
                if (mOperateListener != null) {
                    mOperateListener.onInventorySelection();
                }
                break;
            case R.id.item_service_item_select_template:
                if (mOperateListener != null) {
                    mOperateListener.onSelectTemplate();
                }
                break;
            case R.id.item_service_item_indicator:

                break;
            case R.id.rl_service_item_title:
                if (mOperateListener != null) {
                    mOperateListener.onRequestFocus();
                }
                if (position == 0) {
                    switchViewVisiblility(mServiceRecyclerContainer,mServiceIndicator);
                } else if (position == 1) {
                    switchViewVisiblility(mGoodsRecyclerContainer,mGoodsIndicator);
                }
                break;
            case R.id.rl_pressure_title:
                if (mOperateListener != null) {
                    mOperateListener.onRequestFocus();
                }
                switchViewVisiblility(mLlPressureTestContainer,mPressureIndicator);
                break;

            default:
                break;
        }


    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

    }


    public interface OnOperateListener {
        /**
         * get a new Object to insert
         *
         * @return the object to insert
         */
        GoodsOrder onServiceContentAddTo();

        GoodsOrder onInventorySelection();

        GoodsOrder onSelectTemplate();

        void showServiceEditDialog(int position,GoodsOrder order);

        void showGoodsEditDialog(int position,GoodsOrder order);

        /**
         * the view that will expand or colapsed when the other edittext has keep focus should request focus.
         */
        void onRequestFocus();

    }

    public void setOnOperateListener(OnOperateListener listener) {
        mOperateListener = listener;
    }

    public void removeOperateListener() {
        mOperateListener = null;
    }

    /**
     * 切换视图的显示和隐藏
     *
     * @param view
     */
    private void switchViewVisiblility(View view,ImageView indicator) {
        if (view.getVisibility() == View.GONE) {
            view.setVisibility(View.VISIBLE);
            indicator.setImageResource(R.drawable.ic_expand_false);
        } else {
            view.setVisibility(View.GONE);
            indicator.setImageResource(R.drawable.ic_expand_true);
        }
    }


    public void setGoodsData(int index,GoodsOrder goodsOrder){
        mGoodsInfoAdapter.setData(index,goodsOrder);
    }

    public void setServiceItemData(int index,GoodsOrder goodsOrder){
        mServiceItemAdapter.setData(index,goodsOrder);
    }
}
